package vis.app.components;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultCaret;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;

import vis.app.controller.CodeController;

public class PanelCodeDisplay extends JPanel implements IPanelComponent {

	private static final long serialVersionUID = -8084764128765600019L;

	private JTextArea codeArea;
	private JTextArea variablesArea;

	private Highlighter.HighlightPainter painter;

	@Override
	public void init() {
		setLayout(new BorderLayout());

		initCodeArea();
		initVariableTable();
	}

	private void initCodeArea() {
		codeArea = new JTextArea();
		codeArea.setEditable(false);
		codeArea.setTabSize(2);
		codeArea.setFont(new Font("Consolas", Font.PLAIN, 12));

		DefaultCaret caret = (DefaultCaret) codeArea.getCaret();
		caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

		JScrollPane scrollPane = new JScrollPane(codeArea);
		scrollPane.setPreferredSize(new Dimension(300, 500));
		add(scrollPane, BorderLayout.NORTH);

		painter = new DefaultHighlighter.DefaultHighlightPainter(Color.orange);
	}

	public void removeHighlights() {
		codeArea.getHighlighter().removeAllHighlights();
	}

	public void highlight(String[] lines) {
		removeHighlights();

		for (String line : lines) {
			int lineNumber = Integer.parseInt(line);
			try {
				int startIndex = codeArea.getLineStartOffset(lineNumber);
				int endIndex = codeArea.getLineEndOffset(lineNumber);
				codeArea.getHighlighter().addHighlight(startIndex, endIndex, painter);
			} catch (BadLocationException e) {
				e.printStackTrace();
			}
		}

		// TODO minhvv cheat here
		showDebugVar();
	}

	public void showCode() {
		codeArea.setText("");
		for (String line : CodeController.getInstance().codeText) {
			codeArea.append(line);
			codeArea.append("\n");
		}

		// FIXME minhvv avoid hardcode here
		CodeController.getInstance().executeOneHighlightJob();
	}

	private void initVariableTable() {
		variablesArea = new JTextArea();
		variablesArea.setEditable(false);
		variablesArea.setTabSize(2);
		variablesArea.setFont(new Font("Consolas", Font.PLAIN, 16));

		DefaultCaret caret = (DefaultCaret) variablesArea.getCaret();
		caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

		JScrollPane scrollPane = new JScrollPane(variablesArea);
		scrollPane.setPreferredSize(new Dimension(300, 150));
		add(scrollPane, BorderLayout.SOUTH);
		variablesArea.append("HOPE");

	}

	public void showDebugVar() {
		variablesArea.setText("");
		HashMap<String, String> debugVars = CodeController.getInstance().debugVariables;
		if (null != debugVars && !debugVars.isEmpty()) {
			for (Map.Entry<String, String> e : debugVars.entrySet()) {
				String debugLine = e.getKey() + "\t\t\t" + e.getValue();
				variablesArea.append(debugLine);
				variablesArea.append("\n");
			}
		} else {
			variablesArea.append("NO DEBUGING VARIABLES");
		}
	}

}
